import {createRouter, createWebHistory} from 'vue-router'
import $localStorage from "@/api/storage/localStorage";

const routers = [
    {
        path: '/',
        name: 'index',
        meta: {title: "校园墙"},
        component: () => import('@/view/index/index')
    },
    {
        path: '/login',
        name: '/login',
        meta: {title: '登录'},
        component: () => import('@/view/login/login')
    },
    {
        path: '/taskCenter',
        name: '/taskCenter',
        meta: {title: '接单中心'},
        component: () => import('@/view/task/takeOrders')
    },
    {
        path: '/register',
        name: '/register',
        meta: {title: '注册'},
        component: () => import('@/view/register/register')
    },
    {
        path: '/userCenter',
        name: '/userCenter',
        meta: {
            title: '个人中心'
        },
        component: () => import('@/view/user/user'),
        children: [
            {
                path: '/userCenter/selfInfo',
                name: '/userCenter/selfInfo',
                meta: {
                    title: '个人中心'
                },
                component: () => import('@/view/user/selfInfo')
            },
            {
                path: '/userCenter/myReceiveTask',
                name: '/userCenter/myReceiveTask',
                meta: {
                    title: '我的接单'
                },
                component: () => import('@/view/user/myReceiveTask')
            },
            {
                path: '/userCenter/myTalkList',
                name: '/userCenter/myTalkList',
                meta: {
                    title: '我的说说'
                },
                component: () => import('@/view/user/myTalkList')
            },
            {
                path: '/userCenter/mySendTask',
                name: '/userCenter/mySendTask',
                meta: {
                    title: '我的发单'
                },
                component: () => import('@/view/user/mySendTask')
            }
        ]
    },
    {
        path: '/test',
        name: '/test',
        meta: {title: 'test'},
        component: () => import('@/view/test')
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes: routers
})
router.beforeEach((to, from) => {
    if (to.meta.title) {//判断是否有标题
        document.title = to.meta.title
    }
    if (to.path === '/login') {
        return true
    }
    const token = $localStorage.get("token");
    if (token == null) {
        router.push('/login')
    } else {
        return true
    }
    return true
})
export default router;
